home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / PrimeSpiral / Source / SpiralGenerator.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.1 KB  |  62 lines

  1. /* File:    SpiralGenerator.m - Spiral generator for 'PrimeSpiral'
  2.  *
  3.  * By:        Christopher Lane
  4.  *        Symbolic Systems Resources Group
  5.  *        Knowledge Systems Laboratory
  6.  *        Stanford University
  7.  *
  8.  * Date:    24 February 1990
  9.  *
  10.  * Copyright:    1990 by The Leland Stanford Junior University.  This program
  11.  *        may be distributed without restriction for non-commercial use.
  12.  */
  13.  
  14. #ifdef DEBUG
  15. #import <stdio.h>
  16. #endif
  17. #import "SpiralGenerator.h"
  18.  
  19. @implementation SpiralGenerator
  20.  
  21. + new
  22. {
  23.     self = [super new];
  24.     
  25.     count = 1;
  26.     remain = 2;
  27.     distance = 1;
  28.     direction = RIGHT;
  29.     
  30.     return self;
  31. }
  32.     
  33. - generate:(unsigned int) number :(NXPoint *) point
  34. {
  35.     int dx = 0, dy = 0;
  36.     
  37.     for(; count <= number; count++) {
  38.         if(!--remain) {
  39.             switch(direction) {
  40.                 case UP: distance++; direction = LEFT; break;
  41.                 case DOWN: distance++;
  42.                 default: direction--; break;
  43.                 }
  44.             remain = distance;
  45.             }
  46.         switch(direction) {
  47.             case LEFT: --dx; break;
  48.             case RIGHT: ++dx; break;
  49.             case UP: ++dy; break;
  50.             case DOWN: --dy; break;
  51.             }
  52.         }
  53.     point->x += (float) dx;
  54.     point->y += (float) dy;
  55. #ifdef DEBUG
  56.     (void) printf("\t(%g, %g)\n", point->x, point->y);
  57. #endif
  58.     return self;
  59. }
  60.  
  61. @end
  62.